//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.mall.controller;

import com.example.mall.common.api.CommonPage;
import com.example.mall.common.api.CommonResult;
import com.example.mall.dto.DataDto;
import com.example.mall.dto.ScoreDto;
import com.example.mall.mbg.model.Data;
import com.example.mall.mbg.model.Indicator;
import com.example.mall.mbg.model.Score;
import com.example.mall.mbg.model.Supplier;
import com.example.mall.service.DataService;
import com.example.mall.service.ScoreService;
import com.example.mall.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Api(tags = {"ScoreController"}, description = "评分管理")
@Controller
@RequestMapping({"/score"})
public class ScoreController {
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private DataService dataService;
    private static final Logger LOGGER = LoggerFactory.getLogger(ScoreController.class);

    public ScoreController() {
    }

    @ApiOperation("画面：评分列表")
    @RequestMapping(value = {"init"},method = {RequestMethod.GET})
    public String init(Model model, HttpSession session) {
        List<ScoreDto> scoreDtos = new ArrayList();
        List<Score> scores = this.scoreService.listAll();
        scores.forEach((score) -> {
            ScoreDto scoreDto = new ScoreDto(score);
            Supplier supplier = this.supplierService.get(score.getSupplierid());
            scoreDto.setSupplier(supplier);
            scoreDtos.add(scoreDto);
        });
        model.addAttribute("scoreDtos", scoreDtos);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "score-list";
    }

    @ApiOperation("获取所有评分列表")
    @RequestMapping(value = {"listAll"}, method = {RequestMethod.GET})
    @ResponseBody
    public CommonResult<List<ScoreDto>> getScoreList() {
        List<ScoreDto> scoreDtos = new ArrayList();
        List<Score> scores = this.scoreService.listAll();
        scores.forEach((score) -> {
            ScoreDto scoreDto = new ScoreDto(score);
            Supplier supplier = this.supplierService.get(score.getSupplierid());
            scoreDto.setSupplier(supplier);
            scoreDtos.add(scoreDto);
        });
        return CommonResult.success(scoreDtos);
    }

    @ApiOperation("添加评分")
    @RequestMapping(value = {"/create"}, method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult createScore() {
        CommonResult commonResult;
        scoreService.deleteAll();
        List<Score> scoreList = scoreService.getScoreList();
        try {
            for (int i = 0; i < scoreList.size(); i++) {
                Score score = scoreList.get(i);
                score.setScoreid(i+1);
                if (score.getTotalscore().intValue() >= 90) {
                    score.setRatinglevel("A级");
                    score.setRatingdescription("各方面表现均优秀，是我们最值得信赖的合作伙伴");
                } else if (score.getTotalscore().intValue() >= 80
                        && score.getTotalscore().intValue() < 90) {
                    score.setRatinglevel("B级");
                    score.setRatingdescription("各方面表现良好，能够满足我们的基本需求");
                } else if (score.getTotalscore().intValue() >= 70
                        && score.getTotalscore().intValue() < 80) {
                    score.setRatinglevel("C级");
                    score.setRatingdescription("某些方面表现尚可，但存在一些不足之处");
                } else if (score.getTotalscore().intValue() >= 60
                        && score.getTotalscore().intValue() < 70) {
                    score.setRatinglevel("D级");
                    score.setRatingdescription("各方面表现较差，难以满足我们的需求");
                } else if (score.getTotalscore().intValue() < 60) {
                    score.setRatinglevel("E级");
                    score.setRatingdescription("各方面表现极差，几乎无法满足我们的需求");
                }
                score.setScoredate(new Date());
                score.setCreatetime(score.getScoredate());
                score.setUpdatetime(score.getScoredate());
                this.scoreService.create(score);
            }
            commonResult = CommonResult.success(scoreList);
            LOGGER.debug("createScore success:{}", scoreList);
        } catch (Exception e) {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createScore failed:{}", scoreList);
        }
        return commonResult;
    }

    @ApiOperation("更新指定id评分信息")
    @RequestMapping(
            value = {"/update/{id}"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult updateScore(@PathVariable("id") Integer scoreid, @RequestBody Score score, BindingResult bindingResult) {
        int count = this.scoreService.update(scoreid, score);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success(score);
            LOGGER.debug("updateScore success:{}", score);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateScore failed:{}", score);
        }

        return commonResult;
    }

    @ApiOperation("删除指定id的评分")
    @RequestMapping(
            value = {"/delete/{id}"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult deleteScore(@PathVariable("id") Integer scoreid) {
        int count = this.scoreService.delete(scoreid);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success((Object)null);
            LOGGER.debug("deleteScore success:id={}", scoreid);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("deleteScore failed:id={}", scoreid);
        }

        return commonResult;
    }

    @ApiOperation("分页查询评分列表")
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<CommonPage<Score>> listScore(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize) {
        List<Score> scoreList = this.scoreService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(scoreList));
    }

    @ApiOperation("获取指定id的评分详情")
    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<Score> score(@PathVariable("id") Integer scoreid) {
        return CommonResult.success(this.scoreService.get(scoreid));
    }
}
